package com.cssl.filter;


import org.springframework.context.annotation.Bean;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.context.annotation.Configuration;

import java.util.Collections;


@Configuration
public class CorsFilter{
    @Bean
    public CorsWebFilter corsWebFilter() {
        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();

        //配置跨域
        corsConfiguration.addAllowedHeader("*");     //允许任意请求头跨域
        corsConfiguration.addAllowedMethod("*");     //允许任何请求方式跨域
        corsConfiguration.setAllowedOriginPatterns(Collections.singletonList("*"));     //允许任意请求来源跨域
        corsConfiguration.setAllowCredentials(true); //允许携带cookie信息跨域
        corsConfiguration.setMaxAge(3600L);          //准备响应前的缓存持续的最大时间（以秒为单位）

        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsWebFilter(urlBasedCorsConfigurationSource);
    }

}
